<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    //在箭头函数中，this引用的是定义箭头函数的执行环境
    this.name = "这是window中的name属性";
    let t1 = () => {
        console.log("t1");
        console.log(this.name);
    }

    t1();

    function t2() {
        this.t21 = () => {
            console.log(this.name);
            let t211 = () => {
                console.log(this.name);
            }
            t211();
            function t212() {
                console.log(this.name);
            }
            t212();
        }
        if (!new.target) t21();
    }

    console.log("-------------1-----------")
    t2();

    let tt = new t2();
    console.log("-------------2-----------")
    tt.t21();
    console.log("-------------3-----------")
    tt.name = "这是tt对象";
    tt.t21();

    function t5(callback) {
        console.log("t5");
        callback();//this指向window
    }

    console.log("-------------4----------")
    t5(() => console.log(this.name));

    console.log("-------------5----------")
    let obj2 = {
        name: "这是obj2",
        say: (callback) => {//箭头函数
            console.log(this.name);
            callback();
        },
        sayHello(callback) {//常规函数
            console.log("sayHello");
            console.log(this.name);
            callback();
        }
    }

    let obj3 = {
        name: "这是obj3",
        sayHello:() => {
            console.log("sayHello");
            console.log(this.name);
        },
        say() {
            console.log("sayHello");
            console.log(this.name);
        }
    }
    //箭头函数做为回调函数
    obj2.say(obj3.sayHello);
    console.log("-------------6----------")
    obj2.sayHello(obj3.sayHello);
    //常规函数做为回调函数
    console.log("-------------7----------")
    obj2.say(obj3.say);
    console.log("-------------8----------")
    obj2.sayHello(obj3.say);

    console.log("-------------9----------")
    function t6(){
        this.name = "t6";
        this.t61 = () => {
            console.log(this.name);
        }
    }
    let objT6 = new t6();
    objT6.t61();

    //注意，箭头函数不可以做为构造函数
    console.log("-------------10----------")
    let t7 = () => {
        this.name = "t7";
        this.t71 = () => {
            console.log(this.name);
        }
        t71();
    }
    // let objT7 = new t7();
    // objT7.t71();
    t7();
    console.log(window.name);
</script>
</body>
</html>